{% extends "portal/form.html" %}
{% load i18n %}

{% block myjavascript %}
<script type="text/javascript">
    var userid;
    var displayname;
    var password1;
    var password2;
    var role;
    var pds;
    var sds;
    var email;
    var phone;
    var desc;
    var errormsg;

    function verification() {
        if (password0.length <=0 ) {
            $('.alert-danger').text('旧密码不能为空!');
            return false;
        }

        if (password1.length <= 0 || password2.length <= 0) {
            $('.alert-danger').text('新密码不能为空!');
            return false;
        }

        if ( password1 != password2 ) {
            $('.alert-danger').text('密码不匹配!');
            return false;
        }
        return true;
    }

    function submit_resources() {
        uid = $("#uid").text();
        password0  = $('#password0').val();
        password1  = $('#password1').val();
        password2  = $('#password2').val();

        if (verification() == true) {
            url = "/portal/api/1.0/account/reset_password";
            postdata = {
                userid:         uid,
                oldpassword:    password0,
                newpassword:    password1,
            }

            $.post(url, postdata, function(data) {
                var items = [];
                $.each(data, function (key, val) {
                    items[key] = val;
                });
                if (items['Result'] == "OK") {
                    $('.alert-success').css('display', 'block');
                    $('.alert-danger').css( 'display', 'none');
                    $('.btn').addClass('disabled');
                } else {
                    $('.alert-success').css('display', 'none');
                    $('.alert-danger').text(items['errormsg']);
                    $('.alert-danger').css( 'display', 'block');
                }
            });
        } else {
            $('.alert-success').css('display', 'none');
            $('.alert-danger').css( 'display', 'block');
        }
    }

    $(document).ready(function () {
        $("#mysubmit").click(function(){
            submit_resources();
        });

        $("#myreset").click(function(){
            location.reload();
        });

    });
</script>
{% endblock %}

{% block myform %}
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
    <h2>重置管理员密码</h2>
</div>
<div class="panel-body">
    <div class="row">
        <div class="col-lg-6">
            <!-- /.col-lg-6 (nested) -->
            <div class="col-lg-6">
                <div role="form">
                    <div id="uid" style="display: none">{{ uid }}</div>


                    <div class="form-group">
                        <label>旧的密码</label>
                        <input id="password0" class="form-control" type="password" placeholder="password" value>
                    </div>

                    <div class="form-group">
                        <label>新的密码</label>
                        <input id="password1" class="form-control" type="password" placeholder="password" value>
                    </div>
                    <div class="form-group">
                        <label>重复新的密码</label>
                        <input id="password2" class="form-control" type="password" placeholder="password" value>
                    </div>

                    <button id="mysubmit" type="submit" class="btn btn-primary">提交</button>
                    <button id="myreset"  type="reset"  class="btn btn-default">清除</button>

                </div>
                <p></p>
                <div class="alert alert-success" style="display: none">
                       重置管理员密码成功!
                </div>
                <div class="alert alert-danger" style="display: none">
                      重置管理员密码失败!
                </div>
        </div>
        <!-- /.col-lg-6 (nested) -->
    </div>
    <!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
{% endblock %}
